// pages/tiku/list.js
//获取应用实例
const app = getApp()
var isShow = '';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        station_type: '',
        video_items: [],
        video_admin: '',
    },

    /**
     * 生命周期函数--监听页面加载1
     */
    onLoad: function (options) {
        //console.log('List页面onLoad方法执行1s')
        var that = this;
        try {
            //缓存获取用户信息
            var empinfo = wx.getStorageSync('empinfo');
            if (empinfo) {
                that.setData({
                    //用户类型赋值
                    station_type: empinfo.stationType,
                    video_admin: empinfo.adminMobile,
                  });
                //视频任务主表ID
                var learnVideoTaskId = options.id;
                if(null == learnVideoTaskId || "" == learnVideoTaskId){
                    wx.showToast({
                        title: '未获取到视频任务，请稍后再试！',
                        icon: 'none',
                        duration: 2000
                      })
                }else{
                    wx.setStorageSync('videoTaskId', learnVideoTaskId),
                    //获取身份校验控制开关
                    app.formPost('/wx/video/is_identity_check', {
                  }).then(res => {
                      if (res.code == 200) {
                          wx.setStorageSync('isIdentityCheck', res.isIdentityCheck)
                        //console.log('身份校验开关为：'+res.isIdentityCheck)
                      }
                  }).catch(e => {
                      wx.showModal({
                      title: e
                      })
                  })
                    //获取视频数据
                    app.formPost('/wx/video/video_item', {
                        'employeeId': empinfo.employeeId,
                        'learnVideoTaskId': learnVideoTaskId
                    }).then(res => {
                        if (res.code == 200) {
                        that.setData({
                            video_items: res.video_items,
                        })
                        }
                    }).catch(e => {
                        wx.showModal({
                        title: e
                        })
                    })
                }
            }else{
                wx.reLaunch({
                    url: '/pages/user/bind/index',
                  });
            }
          } catch (e) {
            wx.reLaunch({
                url: '/pages/user/bind/index',
              });
          }
    },
//开始学习
startxuexi: function (event){
    //学习视频记录子表ID
    var video_record_item_id = event.currentTarget.dataset.id;
    if(null == video_record_item_id || "" == video_record_item_id){
        wx.showToast({
            title: '网络异常，未获取到学习数据，请稍后再试！startxuexi',
            icon: 'none',
            duration: 2000
        })
    }else{
        //进入视频播放界面
        wx.navigateTo({
        //   url: '/pages/tiku/kaoti_timu?id=' + video_record_item_id + '&url=' + video_url + '&videoname=' + videoName,
          url: '/pages/tiku/kaoti_timu?id=' + video_record_item_id,
        })
    }
},
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {
       // console.log('List页面onReady方法执行')
    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function (options) {
        //console.log('List页面onShow方法执行')
        //console.log('List页面onShow方法执行时isShow的值为：'+isShow)
        if(isShow == 1){
            var that = this;
            try {
                //缓存获取用户信息
                var empinfo = wx.getStorageSync('empinfo');
                var learnVideoTaskId = wx.getStorageSync('videoTaskId');
                if (empinfo) {
                    that.setData({
                        //用户类型赋值
                        station_type: empinfo.stationType
                    });
                    if(learnVideoTaskId){
                        //获取视频数据
                        app.formPost('/wx/video/video_item', {
                            'employeeId': empinfo.employeeId,
                            'learnVideoTaskId': learnVideoTaskId
                        }).then(res => {
                            //console.log(res.video_items)
                            if (res.code == 200) {
                            that.setData({
                                video_items: res.video_items,
                                //video_item: res.video_items[0]
                            })
                            }
                        }).catch(e => {
                            wx.showModal({
                            title: e
                            })
                        })
                    }else{
                        wx.showToast({
                            title: '网络异常，未获取到视频任务ID，请稍后再试！',
                            icon: 'none',
                            duration: 2000
                        })
                    }
                    
                }else{
                    wx.showToast({
                        title: '网络异常，未获取到用户信息，请稍后再试！',
                        icon: 'none',
                        duration: 2000
                    })
                }
              } catch (e) {
                wx.showToast({
                    title: '网络异常，未获取到用户信息，请稍后再试！',
                    icon: 'none',
                    duration: 2000
                })
              }
        }
        

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {
        //console.log('List页面onHide方法执行')
        isShow = 1;
    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {
        //console.log('List页面onUnload方法执行')
    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})